package handlers

import (
	"cargo-master/cache"
	"cargo-master/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func RoomAdd(c *gin.Context) {
	room_code := c.PostForm("room_code")
	room_name := c.PostForm("room_name")
	room_num := c.PostForm("room_num")

	data := models.Room{
		RoomCode: room_code,
		RoomName: room_name,
		RoomNum:  room_num,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	order_no := c.PostForm("order_no")
	mid, _ := strconv.Atoi(c.PostForm("mid"))

	data := models.Order{
		OrderNo: order_no,
		Mid:     mid,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var orderList []models.OrderList
	/*
			SELECT
		  orders.id AS OrderId,
		  orders.order_no AS OrderNo,
		  rooms.room_code AS RoomCode,
		  rooms.room_name AS RoomName,
		  rooms.room_num AS RoomNum
		FROM
		  orders
		  LEFT JOIN rooms ON orders.mid = rooms.id
	*/
	redis_key := "list_page" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id AS OrderId",
				"orders.order_no AS OrderNo",
				"rooms.room_code AS RoomCode",
				"rooms.room_name AS RoomName",
				"rooms.room_num AS RoomNum",
			).Joins("LEFT JOIN rooms ON orders.mid = rooms.id").
			Offset(offset).Limit(size).Find(&orderList)
		jsonData, _ := json.Marshal(orderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}
	var total int64
	models.DB.Model(&models.Order{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"data":  orderList,
		"msg":   "ok",
		"total": total,
	})
	return
}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")
	var orderList []models.OrderList
	/*
			SELECT
		  orders.id AS OrderId,
		  orders.order_no AS OrderNo,
		  rooms.room_code AS RoomCode,
		  rooms.room_name AS RoomName,
		  rooms.room_num AS RoomNum
		FROM
		  orders
		  LEFT JOIN rooms ON orders.mid = rooms.id
	*/
	redis_key := "detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id AS OrderId",
				"orders.order_no AS OrderNo",
				"rooms.room_code AS RoomCode",
				"rooms.room_name AS RoomName",
				"rooms.room_num AS RoomNum",
			).Joins("LEFT JOIN rooms ON orders.mid = rooms.id").
			Where("orders.id = ?", id).Find(&orderList)
		jsonData, _ := json.Marshal(orderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}
	var total int64
	models.DB.Model(&models.Order{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"data":  orderList,
		"msg":   "ok",
		"total": total,
	})
	return
}
